package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.manager.service.IChatRoomService;
import com.codedancer.campus.manager.service.bo.chatRoom.ChatRoomPageSearchBo;
import com.codedancer.campus.manager.service.vo.chatRoom.ChatRoomPageVo;
import com.codedancer.campus.manager.service.vo.chatRoom.ChatRoomVo;
import com.codedancer.campus.mapper.ChatRoomMapper;
import com.codedancer.campus.mapper.SchoolMapper;
import com.codedancer.campus.mapper.UserSchoolMapper;
import com.codedancer.campus.po.ChatRoom;
import com.codedancer.campus.po.School;
import com.codedancer.campus.po.UserSchool;
import com.codedancer.campus.qo.chatRoom.ChatRoomPageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class ChatRoomServiceImpl implements IChatRoomService {

    @Autowired
    private ChatRoomMapper chatRoomMapper;
    @Autowired
    private UserSchoolMapper userSchoolMapper;
    @Autowired
    private SchoolMapper schoolMapper;

    /**
     * 分页查询聊天室
     *
     * @param chatRoomPageSearchBo
     * @return
     */
    @Override
    public IPage<ChatRoomPageVo> page(ChatRoomPageSearchBo chatRoomPageSearchBo) {
        ChatRoomPageSearchQo chatRoomPageSearchQo = BeanUtil
            .copyProperties(chatRoomPageSearchBo, ChatRoomPageSearchQo.class);
        IPage<ChatRoom> chatRoomIPage = chatRoomMapper.page(
            new Page<ChatRoom>(chatRoomPageSearchBo.getPage(), chatRoomPageSearchBo.getSize()),
            chatRoomPageSearchQo);
        IPage<ChatRoomPageVo> chatRoomPageVoIPage = new Page<>();
        List<ChatRoomPageVo> chatRoomPageVos;
        chatRoomPageVos = chatRoomIPage.getRecords().stream().map(chatRoom -> {
            ChatRoomPageVo chatRoomPageVo = BeanUtil.copyProperties(chatRoom, ChatRoomPageVo.class);
            chatRoomPageVo.setHouseUserName(chatRoom.getHouseOwnerUser().getNickName());
            chatRoomPageVo.setChatUserName(chatRoom.getChatUser().getNickName());
            return chatRoomPageVo;
        }).collect(Collectors.toList());
        chatRoomPageVoIPage.setRecords(chatRoomPageVos);
        chatRoomPageVoIPage.setTotal(chatRoomIPage.getTotal());
        return chatRoomPageVoIPage;
    }

    /**
     * 详情
     *
     * @param uuid
     * @return
     */
    @Override
    public ChatRoomVo detail(String uuid) {
        ChatRoom chatRoom = chatRoomMapper.detail(uuid);
        if (ObjectUtil.isEmpty(chatRoom)) {
            throw new BusinessException("聊天室不存在");
        }
        ChatRoomVo chatRoomVo = BeanUtil.copyProperties(chatRoom, ChatRoomVo.class);
        chatRoomVo.setHouseUserName(chatRoom.getHouseOwnerUser().getNickName());
        chatRoomVo.setHouseUserUuid(chatRoom.getHouseOwnerUser().getUuid());
        chatRoomVo.setChatUserName(chatRoom.getChatUser().getNickName());
        chatRoomVo.setChatUserUuid(chatRoom.getChatUser().getUuid());
        // 查询学校
        UserSchool userSchool = userSchoolMapper
            .selectOneByColumn("user_uuid", chatRoom.getHouseOwnerUuid());
        School school = schoolMapper.selectById(userSchool.getSchoolUuid());
        chatRoomVo.setSchoolName(school.getName());
        return chatRoomVo;
    }
}
